package com.java.gulimall.order.listener;

import com.java.gulimall.order.entity.OrderEntity;
import com.java.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author jl
 * Created on 2020/9/21
 */
@RabbitListener(queues = "order.release.order.queue")
@Service
@Slf4j
public class OrderCloseListener {
    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void testListener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        log.info("收到超时未支付的订单,准备关单.订单编号"+orderEntity.getOrderSn());
        try {
            orderService.closeOrder(orderEntity);
            // todo 调用支付宝收单,订单已取消不能付款
            // 手动确认收到消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
